<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

            <el-form-item label="日期查询">
                <el-date-picker v-model="value" clearable @clear="getList" type="daterange" value-format="yyyy-MM-dd"
                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
            </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                    @click="handleUpdate">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <el-table v-loading="loading" :data="basicauthorList" @selection-change="handleSelectionChange">
            <!-- <el-table-column width="55" align="center" /> -->
            <!-- <el-table-column label="主键id" align="center" prop="authorId" /> -->
            <el-table-column label="设备名称" align="center" prop="facility_name" />
            <el-table-column label="报警次数" align="center" prop="alarm1" />
            <el-table-column label="报警时长" align="center" prop="alarm0_label" />
            <el-table-column label="初始化完成时间" align="center" prop="initialize_done0_label" />
            <el-table-column label="初始化次数" align="center" prop="initialize_done1" />
            <el-table-column label="未初始化时间" align="center" prop="initialize_not0_label" />
            <el-table-column label="未初始化次数" align="center" prop="initialize_not1" />
            <el-table-column label="未运行时间" align="center" prop="not_run0_label" />
            <el-table-column label="未运行次数" align="center" prop="not_run1" />
            <el-table-column label="运行时间" align="center" prop="service0_label" />
            <el-table-column label="运行次数" align="center" prop="service1" />
            <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column> -->
        </el-table>

        <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" /> -->

        <!-- 添加或修改作者对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="作者编码" prop="authorCode">
                    <el-input v-model="form.authorCode" placeholder="请输入作者编码" />
                </el-form-item>
                <el-form-item label="作者朝代-可选择多个" prop="authorDynasty">
                    <el-input v-model="form.authorDynasty" placeholder="请输入作者朝代-可选择多个" />
                </el-form-item>
                <el-form-item label="作者朝代年份" prop="authorBeforeChrist">
                    <el-input v-model="form.authorBeforeChrist" placeholder="请输入作者朝代年份" />
                </el-form-item>
                <el-form-item label="作者生卒年份" prop="authorBirthAndDeath">
                    <el-input v-model="form.authorBirthAndDeath" placeholder="请输入作者生卒年份" />
                </el-form-item>
                <el-form-item label="作者图片" prop="authorPhoto">
                    <el-input v-model="form.authorPhoto" placeholder="请输入作者图片" />
                </el-form-item>
                <el-form-item label="作者履历" prop="authorResume">
                    <el-input v-model="form.authorResume" placeholder="请输入作者履历" />
                </el-form-item>
                <el-form-item label="状态" prop="authorState">
                    <el-input v-model="form.authorState" placeholder="请输入状态" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listBasicauthor, getBasicauthor, delBasicauthor, addBasicauthor, updateBasicauthor } from "@/api/statistics";

export default {
    name: "Basicauthor",
    data() {
        return {
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            value: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 作者表格数据
            basicauthorList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                start: null,
                finish: null,

            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {

        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth() + 1; // 月份从0开始，所以要加1
        var day = today.getDate();
        // 格式化日期
        var formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
        console.log(formattedDate);
        this.queryParams.start = formattedDate
        this.queryParams.finish = formattedDate
        this.value = [formattedDate, formattedDate]
        this.getList()
    },
    methods: {
        /** 查询作者列表 */
        getList() {
            this.loading = true;
            listBasicauthor(this.queryParams).then(response => {
                console.log(response);
                this.basicauthorList = response.data.data;
                // this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                authorId: null,
                authorCode: null,
                authorDynasty: null,
                authorBeforeChrist: null,
                authorBirthAndDeath: null,
                authorPhoto: null,
                authorResume: null,
                authorState: null,
                createTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.start = this.value == null ? null : this.value.length > 0 ? this.value[0] : null
            this.queryParams.finish = this.value == null ? null : this.value.length > 0 ? this.value[1] : null
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.authorId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加作者";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const authorId = row.authorId || this.ids
            getBasicauthor(authorId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改作者";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.authorId != null) {
                        updateBasicauthor(this.form).then(response => {
                            console.log(response);
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addBasicauthor(this.form).then(response => {
                            console.log(response);
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const authorIds = row.authorId || this.ids;
            this.$modal.confirm('是否确认删除作者编号为"' + authorIds + '"的数据项？').then(function () {
                return delBasicauthor(authorIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/basicauthor/export', {
                ...this.queryParams
            }, `basicauthor_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>